import java.io.*;

public class ObjectInputOutputStream {
    //对象流的使用
    public static void main(String[] args) {

        //序列化
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("IO/file/object.dat"));
//            oos.writeObject(new String("Hello World!"));
//            oos.flush(); // 刷新操作

            oos.writeObject(new Person("张三",23));
            oos.flush(); // 刷新操作

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(oos!=null)
                    oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //反序列化: 将磁盘中的对象还原为内存中的一个Java对象
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("IO/file/object.dat"));
            Object obj = ois.readObject();
            Person p = (Person) obj;
            System.out.println(p);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ois!=null)
                    ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
